<?php

namespace app\home\controller;

use think\Db;

class Productmain extends Base
{
    protected $beforeActionList = [
        'first',
    ];

    protected function first()
    {
        if (session('username')) {
            $this->assign('nav', 'user');
        } else {
            $this->redirect('/home/user/index');
        }
    }

    /*管理产品*/
    public function index()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {

           $info= DB::name('product_run')->where('diy_domain', $post['diy_domain'])->find();
            if ($info && $info['id']!=$post['id'] && !empty($post['diy_domain'])) {
                $this->error('自定义域名已经使用，请更换其他域名');
            }
            if (DB::name('product_run')->where('id',$post['id'])->update(['diy_domain'=>$post['diy_domain'],'info'=>$post['info']])) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');


        } else {
            if (isset($post['id']) && !empty($post['id'])) {
                $info = DB::name('product_run')->where('id', $post['id'])->where('uid',session('uid'))->find();
                $info['product_title'] = DB::name('product')->where('id', $info['product_id'])->value('title');
                $info['e_time']=date('Y-m-d',$info['e_time']);
                $info['s_time']=date('Y-m-d',$info['s_time']);
                $this->assign('info', $info);
            } else {
                $this->error('参数错误，请稍后再试');
            }
            return view();
        }
    }

    /*产品列表*/
    public function readmin()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $data['username']='1'.mt_rand(100000,999900).mt_rand(1000,9999);

            $mCode = array('<','>','!','@','#','{','}','~','2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $data['password']= '';
            foreach (array_rand($mCode, 6) as $key) {
                $data['password'].= $mCode[$key];
            }
            $data['readmin']=2;
            if (DB::name('product_run')->where('id', $post['id'])->update($data)) {
                 $this->success('重设操作成功，请10分钟后使用新用户名和密码登录!','',json_encode($data));
            }
            $this->error('操作失败');


        }
    }

}